package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.page.PageQuery2;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.system.domain.TDeviceV2Input;
import com.ruoyi.system.service.TDeviceV2InputService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.ValidationException;
import java.util.List;

/**
 * @author liangjiuqing
 * @since 2024-09-27 10:14
 */
@Api(tags = "设备管理-设备录入")
@RestController
@RequestMapping("/deviceV2Input")
public class TDeviceV2InputController {
    @Autowired
    private TDeviceV2InputService deviceV2InputService;

    @ApiOperation("分页查询设备录入列表")
    @GetMapping("/listPageDeviceV2Inputs")
    public ResultEntity<IPage<TDeviceV2Input>> listPageDeviceV2Inputs(DeviceV2InputPageQuery deviceV2InputPageQuery) {
        Page<TDeviceV2Input> page = deviceV2InputService.page(deviceV2InputPageQuery.buildPage(),
                Wrappers.<TDeviceV2Input>lambdaQuery()
                        .eq(deviceV2InputPageQuery.getProjectId() != null, TDeviceV2Input::getProjectId, deviceV2InputPageQuery.getProjectId())
                        .orderByDesc(TDeviceV2Input::getCreateTime));
        return ResultEntity.createSuccess(page);
    }

    @ApiOperation("新增或修改设备录入数据")
    @PostMapping("/saveOrUpdateDeviceV2Input")
    public ResultEntity<Void> saveOrUpdateDeviceV2Input(@Validated @RequestBody TDeviceV2Input deviceV2Input) {
        int count = deviceV2InputService.count(Wrappers.<TDeviceV2Input>lambdaQuery()
                .eq(TDeviceV2Input::getDeviceNum, deviceV2Input.getDeviceNum())
                .ne(deviceV2Input.getId() != null, TDeviceV2Input::getId, deviceV2Input.getId()));
        if (count > 0) {
            throw new ValidationException("设备对接码已存在");
        }
        deviceV2InputService.saveOrUpdate(deviceV2Input);
        return ResultEntity.createSuccess(null);
    }

    @ApiOperation("批量删除设备录入数据")
    @GetMapping("/removeDeviceV2InputByIds/{ids}")
    public ResultEntity<Void> removeDeviceV2InputByIds(@ApiParam(value = "主键ID，逗号分隔", required = true) @PathVariable List<Long> ids) {
        deviceV2InputService.removeByIds(ids);
        return ResultEntity.createSuccess(null);
    }

    @Data
    @EqualsAndHashCode(callSuper = true)
    static class DeviceV2InputPageQuery extends PageQuery2 {
        @ApiModelProperty("项目id")
        private Long projectId;
    }

}
